#include "DaterMainGroupMemberWindow.h"
#include "DaterCommon.h"
#include "DaterBaseWindowManager.h"
#include "DaterSystem.h"

MainGroupMemberWindow::MainGroupMemberWindow(const CEGUI::String& id_name, CEGUI::Window* parent):
MainWindow(parent)
{
	using namespace CEGUI;

	_root = CEGUI::WindowManager::getSingleton().loadWindowLayout("GroupMemberWindow.layout",id_name);

	_name = _root->getName().c_str();

	WindowManager& winMgr = WindowManager::getSingleton();

	ScrollablePane* pane = static_cast<ScrollablePane*>(winMgr.getWindow("GroupMemberWindow/ScrollPane"));
	pane->setContentPaneAutoSized(true);
	Window* namePrototype = winMgr.getWindow("GroupMemberWindow/ScrollPane/MemberName1");
	namePrototype->setRiseOnClickEnabled(false);

	Window* nameToAdded;
	String nameNewName;
	std::string newText;

	for(int i = 2; i < 10; i++){
		nameNewName = "GroupMemberWindow/ScrollPane/MemberName" + PropertyHelper::intToString(i);
		nameToAdded = namePrototype->clone(nameNewName);
		nameToAdded->setYPosition(cegui_reldim(0.1f*(i-1)));
		namePrototype->getParent()->addChildWindow(nameToAdded);
	}

	refreshEvent();

	subscribeEvents();
}

MainGroupMemberWindow::~MainGroupMemberWindow()
{
}

void MainGroupMemberWindow::showWindow()
{
	//_parent->addChildWindow(_root);
	_parent->addChildWindow(_root);
	_root->activate();
}

void MainGroupMemberWindow::hideWindow()
{
	//_parent->removeChildWindow(_root);
	_root->deactivate();
	_parent->removeChildWindow(_root);
}

void MainGroupMemberWindow::subscribeEvents(){
	using namespace CEGUI;

	Window *nameWindow;
	for(int i  = 1; i <= 10; i++)
	{
		nameWindow = WindowManager::getSingleton().getWindow("GroupMemberWindow/ScrollPane/MemberName" + PropertyHelper::intToString(i));

		//nameWindow->subscribeEvent(Window::EventMouseWheel, Event::Subscriber(&MainGroupMemberWindow::MouseWheel, this));

		nameWindow->subscribeEvent(Window::EventMouseClick, Event::Subscriber(&MainGroupMemberWindow::MouseClick,this));
	}
}

bool MainGroupMemberWindow::MouseClick(const CEGUI::EventArgs& args)
{
	using namespace CEGUI;

	float wheelDistance = static_cast<const CEGUI::MouseEventArgs&>(args).wheelChange;

	static_cast<ScrollablePane*>(_root->getChildAtIdx(0))->setVerticalScrollPosition(
		static_cast<ScrollablePane*>(_root->getChildAtIdx(0))->getVerticalScrollPosition() - wheelDistance/27);

	return true;
}

void MainGroupMemberWindow::refreshEvent()
{
}
